package cn.iocoder.yudao.module.system.service.WeChatWorkInterface;

import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
import cn.iocoder.yudao.module.infra.service.config.ConfigService;
import cn.iocoder.yudao.module.system.controller.admin.weChatWorkInterface.ObjectFactory;
import cn.iocoder.yudao.module.system.controller.admin.weChatWorkInterface.TextMsgSend;
import cn.iocoder.yudao.module.system.controller.admin.weChatWorkInterface.WeChatWorkInterface1;
import cn.iocoder.yudao.module.system.controller.admin.weChatWorkInterface.WeChatWorkInterface1Soap;
import cn.iocoder.yudao.module.system.dal.dataobject.seatreserve.SeatReserveDO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.xml.ws.WebServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Service
@Validated
public class TextMsgSendServiceImpl implements TextMsgSendService{

    @Resource
    private ConfigService configService;
    @Override
    public void TextMsgSend(String userCode, SeatReserveDO seatReserve,String template) {
        try {
            //获取消息发送模板
            ConfigDO config = configService.getConfigByKey("textMsgSend-"+template);
            if(config!=null){
                /*获取应用id和消息发送模板*/
                String agentid = config.getValue();
                String templateInfo = config.getRemark();
                // 创建ObjectFactory实例
                ObjectFactory objectFactory = new ObjectFactory();

                //日期类型格式化样式
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                // 使用ObjectFactory创建TextMsgSend请求对象并设置参数
                TextMsgSend textMsgSendRequest = objectFactory.createTextMsgSend();
                textMsgSendRequest.setTouser(userCode); // 请替换为实际的用户ID
                /*目前成功和失败消息发送是一致内容，将来可能不一致*/
                if(template.equalsIgnoreCase("cancel")){//根据取消模板赋值
                    textMsgSendRequest.setContent(String.format(templateInfo, seatReserve.getSeatRegion(),
                            seatReserve.getSeatName(),
                            seatReserve.getReserveDate().format(formatter),
                            seatReserve.getReserveTime().equals("0")?"全天":(
                                    seatReserve.getReserveTime().equals("1")?"上午":"下午"))); // 请替换为实际的消息内容
                }else{//预定成功模板赋值
                    textMsgSendRequest.setContent(String.format(templateInfo, seatReserve.getSeatRegion(),
                            seatReserve.getSeatName(),
                            seatReserve.getReserveDate().format(formatter),
                            seatReserve.getReserveTime().equals("0")?"全天":(
                                    seatReserve.getReserveTime().equals("1")?"上午":"下午"))); // 请替换为实际的消息内容
                }
                textMsgSendRequest.setAgentid(agentid); // 请替换为实际的应用ID

                // 获取WSDL文件的URL
                String wsdlLocation = "file:/home/odoovendor/seatReserve/WeChatWorkInterfacefortextMsgSend.wsdl";
                URL url = new URL(wsdlLocation);

                // 创建WeChatWorkInterface1服务实例
                javax.xml.ws.Service service = new WeChatWorkInterface1(url);

                // 获取WeChatWorkInterface1Soap端口实例
                WeChatWorkInterface1Soap port = service.getPort(WeChatWorkInterface1Soap.class);

                // 调用textMsgSend方法并获取响应
                String response = port.textMsgSend(textMsgSendRequest.getTouser(),
                        textMsgSendRequest.getContent(),
                        textMsgSendRequest.getAgentid());
                // 输出响应结果
                System.out.println("发送消息的响应结果：" + response);
            }
        } catch (WebServiceException | MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void TextMsgSend(String userCode, String area, String type, Date date, String template) {
        try {
            //获取消息发送模板
            ConfigDO config = configService.getConfigByKey("textMsgSend-"+template);
            if(config!=null){
                /*获取应用id和消息发送模板*/
                String agentid = config.getValue();
                String templateInfo = config.getRemark();
                // 创建ObjectFactory实例
                ObjectFactory objectFactory = new ObjectFactory();

                //日期类型格式化样式
                // 创建 SimpleDateFormat 对象，指定目标格式
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

                // 使用 formatter 将 Date 对象格式化为字符串
                String formattedDate = formatter.format(date);
                // 使用ObjectFactory创建TextMsgSend请求对象并设置参数
                TextMsgSend textMsgSendRequest = objectFactory.createTextMsgSend();
                textMsgSendRequest.setTouser(userCode); // 请替换为实际的用户ID
                /*目前成功和失败消息发送是一致内容，将来可能不一致*/
                if(template.equalsIgnoreCase("cancel")){//根据取消模板赋值
                    textMsgSendRequest.setContent(String.format(templateInfo,area,
                            formattedDate,
                            type.equals("all")?"全天":(
                                    type.equals("am")?"上午":"下午"))); // 请替换为实际的消息内容
                }else{//预定成功模板赋值
                    textMsgSendRequest.setContent(String.format(templateInfo,
                            area,
                            formattedDate,
                            type.equals("all")?"全天":(
                                    type.equals("am")?"上午":"下午"))); // 请替换为实际的消息内容
                }
                textMsgSendRequest.setAgentid(agentid); // 请替换为实际的应用ID

                // 获取WSDL文件的URL
                String wsdlLocation = "file:/home/odoovendor/seatReserve/WeChatWorkInterfacefortextMsgSend.wsdl";
                URL url = new URL(wsdlLocation);

                // 创建WeChatWorkInterface1服务实例
                javax.xml.ws.Service service = new WeChatWorkInterface1(url);

                // 获取WeChatWorkInterface1Soap端口实例
                WeChatWorkInterface1Soap port = service.getPort(WeChatWorkInterface1Soap.class);

                // 调用textMsgSend方法并获取响应
                String response = port.textMsgSend(textMsgSendRequest.getTouser(),
                        textMsgSendRequest.getContent(),
                        textMsgSendRequest.getAgentid());
                // 输出响应结果
                System.out.println("发送消息的响应结果：" + response);
            }
        } catch (WebServiceException | MalformedURLException e) {
            e.printStackTrace();
        }
    }
}
